<?php

/**
 * unused
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Resources.php 38 2009-02-18 17:12:39Z monzee $
 */
class Admin_Array_Resources
{
    protected $_resource;

    protected $_resourceNameSpec = ':module/:controller/:action';

    /**
     * Get resource id
     *
     * $params must contain the keys 'module', 'controller' and 'action'
     *
     * @param array $params Uses the current request if not passed
     * @return string
     */
    public function generateResourceId(array $params = null)
    {
        if (null !== $params) {
            $module = $params['module'];
            $controller = $params['controller'];
            $action = $params['action'];
        } else {
            $req = Zend_Controller_Front::getInstance()->getRequest();
            $module = $req->getModuleName();
            $controller = $req->getControllerName();
            $action = $req->getActionName();
        }

        $replaceMap = array(
            ':module' => $module,
            ':controller' => $controller,
            ':action' => $action,
        );

        $this->_resource = str_replace(
            array_keys($replaceMap),
            array_values($replaceMap),
            $this->_resourceNameSpec
        );

        return $this->_resource;
    }

    /**
     *
     * @return array containing role ids
     */
    public function getRolesWithPermissions()
    {
        $resId = $this->_resource;

        $permissionList = new Admin_Array_Permissions();
        $permissions = $permissionList->getByResourceId($resId);

        $ret = array();
        foreach ($permissions as $permission) {
            $ret[] = $permission['role_id'];
        }

        return $ret;
    }

}
